home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Framewrk / FWPart / Sources / FWPrHdlr.cpp < prev    next >
Encoding:
Text File  |  1996-04-25  |  27.4 KB  |  1,079 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPrHdlr.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFrameW.hpp"
  11.  
  12. #ifndef FWPRHDLR_H
  13. #include "FWPrHdlr.h"
  14. #endif
  15.  
  16. #ifndef FWPART_H
  17. #include "FWPart.h"
  18. #endif
  19.  
  20. #ifndef FWFRAME_H
  21. #include "FWFrame.h"
  22. #endif
  23.  
  24. #ifndef FWPRINFO_H
  25. #include "FWPrInfo.h"
  26. #endif
  27.  
  28. #ifndef FWCFMRES_H
  29. #include "FWCFMRes.h"
  30. #endif
  31.  
  32. #ifndef FWPRINT_K
  33. #include "FWPrint.k"
  34. #endif
  35.  
  36. #ifndef FWALERT_H
  37. #include "FWAlert.h"
  38. #endif
  39.  
  40. #ifndef FWGRUTIL_H
  41. #include "FWGrUtil.h"
  42. #endif
  43.  
  44. #ifndef FWMNUBAR_H
  45. #include "FWMnuBar.h"
  46. #endif
  47.  
  48. #ifndef FWEVENT_H
  49. #include "FWEvent.h"
  50. #endif
  51.  
  52. #ifndef FWRESACC_H
  53. #include "FWResAcc.h"
  54. #endif
  55.  
  56. #ifndef FWRESTYP_H
  57. #include "FWResTyp.h"
  58. #endif
  59.  
  60. #ifndef FWODGEOM_H
  61. #include "FWODGeom.h"
  62. #endif
  63.  
  64. #ifndef FWITERS_H
  65. #include "FWIters.h"
  66. #endif
  67.  
  68. #ifndef SLODFSTR_H
  69. #include "SLODFStr.h"
  70. #endif
  71.  
  72. #ifndef SLODFSTR_K
  73. #include "SLODFStr.k"
  74. #endif
  75.  
  76. // ----- OpenDoc includes
  77.  
  78. #ifndef SOM_ODFacet_xh
  79. #include <Facet.xh>
  80. #endif
  81.  
  82. #ifndef SOM_ODSession_xh
  83. #include <ODSessn.xh>
  84. #endif
  85.  
  86. #ifndef SOM_ODArbitrator_xh
  87. #include <Arbitrat.xh>
  88. #endif
  89.  
  90. #ifndef SOM_ODFoci_xh
  91. #include <Foci.xh>
  92. #endif
  93.  
  94. #ifndef SOM_ODFrame_xh
  95. #include <Frame.xh>
  96. #endif
  97.  
  98. #ifndef SOM_ODWindowState_xh
  99. #include <WinStat.xh>
  100. #endif
  101.  
  102. #ifndef SOM_Module_OpenDoc_Commands_defined
  103. #include <CmdDefs.xh>
  104. #endif
  105.  
  106. //========================================================================================
  107. // RunTime Info
  108. //========================================================================================
  109.  
  110. #ifdef FW_BUILD_MAC
  111. #pragma segment odfprinting
  112. #endif
  113.  
  114. //========================================================================================
  115. // Local helpers
  116. //========================================================================================
  117.  
  118. #ifdef FW_BUILD_WIN
  119.  
  120. static FW_Boolean    gWinAbortPrinting = FALSE;
  121. static HWND         gWinProgressDlg = NULL;
  122.  
  123. static HWND    WinGetFrameWindow(
  124.             Environment*    ev,
  125.             ODFrame*        frame);
  126.         
  127.  
  128. static void    WinShowControl(
  129.             HWND        hWnd,
  130.             int            nID,
  131.             BOOL        bShow);
  132.  
  133. static int CALLBACK WinPrintProgessDlgProc(
  134.             HWND        hWnd,
  135.             UINT        message,
  136.             WPARAM        wParam,
  137.             LPARAM        lParam);
  138.  
  139. static BOOL CALLBACK WinAbortProc(
  140.             HDC        hDC,
  141.             int        nCode);
  142.  
  143. #endif
  144.  
  145. #ifdef FW_BUILD_MAC
  146.  
  147. static DialogPtr    gMacProgressDlg = NULL;
  148. static PrIdleUPP    gMacIdleProcUPP = NULL;
  149. static FW_Boolean    gMacPrintAborted = FALSE;
  150.  
  151. static pascal void MacIdleProc();
  152.  
  153. #endif
  154.  
  155. //----------------------------------------------------------------------------------------
  156. //    FW_CPrintHandler::FW_CPrintHandler
  157. //----------------------------------------------------------------------------------------
  158.  
  159. FW_CPrintHandler::FW_CPrintHandler(FW_CPart* part, FW_CFrame* frame) :
  160.     fPart(part),
  161.     fFrame(frame),
  162.     fPrintView(NULL)
  163. {
  164. }
  165.  
  166. //----------------------------------------------------------------------------------------
  167. //    FW_CPrintHandler::~FW_CPrintHandler
  168. //----------------------------------------------------------------------------------------
  169.  
  170. FW_CPrintHandler::~FW_CPrintHandler()
  171. {
  172. }
  173.  
  174. //----------------------------------------------------------------------------------------
  175. //    FW_CPrintHandler::GetPart
  176. //----------------------------------------------------------------------------------------
  177.  
  178. FW_CPart* FW_CPrintHandler::GetPart() const
  179. {
  180.     return fPart;
  181. }
  182.  
  183. //----------------------------------------------------------------------------------------
  184. //    FW_CPrintHandler::GetFrame
  185. //----------------------------------------------------------------------------------------
  186.  
  187. FW_CFrame* FW_CPrintHandler::GetFrame() const
  188. {
  189.     return fFrame;
  190. }
  191.  
  192. //----------------------------------------------------------------------------------------
  193. //    FW_CPrintHandler::GetPrintView
  194. //----------------------------------------------------------------------------------------
  195.  
  196. FW_CView* FW_CPrintHandler::GetPrintView() const
  197. {
  198.     return fPrintView;
  199. }
  200.  
  201. //----------------------------------------------------------------------------------------
  202. //    FW_CPrintHandler::IsCurrentlyPrintable
  203. //----------------------------------------------------------------------------------------
  204.  
  205. FW_Boolean FW_CPrintHandler::IsCurrentlyPrintable(Environment* /* ev */) const
  206. {
  207.     return TRUE;
  208. }
  209.  
  210. //----------------------------------------------------------------------------------------
  211. //    FW_CPrintHandler::DoAdjustMenus
  212. //----------------------------------------------------------------------------------------
  213.  
  214. FW_Boolean FW_CPrintHandler::DoAdjustMenus(Environment *ev, FW_CMenuBar* menuBar, FW_Boolean hasMenuFocus, FW_Boolean isRoot)
  215. {
  216. FW_UNUSED(hasMenuFocus);
  217.     if (isRoot)
  218.     {
  219.         menuBar->EnableCommand(ev, kODCommandPageSetup, TRUE);
  220.         menuBar->EnableCommand(ev, kODCommandPrint, IsCurrentlyPrintable(ev));
  221.     }
  222.  
  223.     return FALSE;
  224. }
  225.  
  226. //----------------------------------------------------------------------------------------
  227. //    FW_CPrintHandler::GetPrintContentExtent
  228. //----------------------------------------------------------------------------------------
  229.  
  230. void FW_CPrintHandler::GetPrintContentExtent(Environment* ev, FW_CPoint& extent) const
  231. {
  232.     fFrame->GetContentExtent(ev, extent);
  233.     FW_ASSERT(extent.x != FW_kFixed0);
  234.     FW_ASSERT(extent.y != FW_kFixed0);
  235. }
  236.  
  237. //----------------------------------------------------------------------------------------
  238. //    FW_CPrintHandler::GetPrintJobTitle
  239. //----------------------------------------------------------------------------------------
  240.  
  241. void FW_CPrintHandler::GetPrintJobTitle(Environment* ev, FW_CString& jobTitle) const
  242. {
  243.     fPart->GetPartName(ev, jobTitle);
  244. }
  245.  
  246. //----------------------------------------------------------------------------------------
  247. //    FW_CPrintHandler::DoMenu
  248. //----------------------------------------------------------------------------------------
  249.  
  250. FW_Boolean FW_CPrintHandler::DoMenu(Environment* ev, const FW_CMenuEvent& theMenuEvent)
  251. {
  252.     FW_Boolean result = TRUE;
  253.     
  254.     switch (theMenuEvent.GetCommandID(ev))
  255.     {
  256.         case kODCommandPageSetup:
  257.             HandlePageSetup(ev);
  258.             break;
  259.  
  260.         case kODCommandPrint:
  261.             HandlePrint(ev);
  262.             break;
  263.  
  264.         default:
  265.             result = FALSE;
  266.     }
  267.     
  268.     return result;
  269. }
  270.  
  271. //----------------------------------------------------------------------------------------
  272. //    FW_CPrintHandler::DoPageSetupDialog
  273. //----------------------------------------------------------------------------------------
  274.  
  275. FW_Boolean FW_CPrintHandler::DoPageSetupDialog(Environment* ev)
  276. {
  277.     FW_Boolean result = FALSE;
  278.  
  279.     if (PreModalDialog(ev))
  280.     {
  281.         FW_CPrintInfo* printInfo = GetPrintInfo();
  282.  
  283. #ifdef FW_SUPPORT_GX
  284.         gxJob job = printInfo->GetGXJob();
  285.         if (job != NULL)
  286.         {
  287.             result = ::GXJobDefaultFormatDialog(job, NULL);
  288.         }
  289.         else
  290.         {
  291. #endif
  292. #ifdef FW_BUILD_WIN
  293.             LPPRINTDLG lppd = printInfo->WinGetPrintDlg()
  294.             lppd->Flags    = PD_PRINTSETUP;
  295.             lppd->hwndOwner = WinGetFrameWindow(ev, fFrame->GetODFrame(ev));
  296.             result = ::PrintDlg(lppd);
  297. #endif
  298. #ifdef FW_BUILD_MAC
  299.             THPrint thPrint = printInfo->MacGetTHPrint();
  300.             result = ::PrStlDialog(thPrint);
  301. #endif
  302. #ifdef FW_SUPPORT_GX
  303.         }
  304. #endif
  305.         PostModalDialog(ev);
  306.     }
  307.     
  308.     return result;
  309. }
  310.  
  311. //----------------------------------------------------------------------------------------
  312. //    FW_CPrintHandler::DoJobDialog
  313. //----------------------------------------------------------------------------------------
  314.  
  315. FW_Boolean FW_CPrintHandler::DoJobDialog(Environment* ev)
  316. {
  317.     FW_Boolean result = FALSE;
  318.  
  319.     if (PreModalDialog(ev))
  320.     {
  321.         FW_CPrintInfo* printInfo = GetPrintInfo();
  322.  
  323. #ifdef FW_SUPPORT_GX
  324.         gxJob job = printInfo->GetGXJob();
  325.         if (job != NULL)
  326.         {
  327.             result = ::GXJobPrintDialog(job, NULL);
  328.         }
  329.         else
  330.         {
  331. #endif
  332. #ifdef FW_BUILD_WIN
  333.             LPPRINTDLG lppd = printInfo->WinGetPrintDlg()
  334.             lppd->Flags    = PD_RETURNDC;
  335.             lppd->hwndOwner = WinGetFrameWindow(ev, fFrame->GetODFrame(ev));
  336.             if (::PrintDlg(lppd))
  337.             {
  338.                 printInfo->WinUpdatePageRect();
  339.                 result = TRUE;
  340.             }
  341. #endif
  342. #ifdef FW_BUILD_MAC
  343.             THPrint thPrint = printInfo->MacGetTHPrint();
  344.             (*thPrint)->prJob.iFstPage = 1;
  345.             (*thPrint)->prJob.iLstPage = 9999;
  346.     
  347.             result = ::PrJobDialog(thPrint);
  348. #endif
  349. #ifdef FW_SUPPORT_GX
  350.         }
  351. #endif
  352.         PostModalDialog(ev);
  353.     }
  354.  
  355.     return result;
  356. }
  357.  
  358. //----------------------------------------------------------------------------------------
  359. //    FW_CPrintHandler::NotifyNoDefaultPrinter
  360. //----------------------------------------------------------------------------------------
  361.  
  362. void FW_CPrintHandler::NotifyNoDefaultPrinter(Environment* ev)
  363. {
  364.     FW_CString partName;
  365.     fPart->GetPartName(ev, partName);
  366.     
  367.     FW_CString message;
  368.     ::FW_PrivLoadODFString(ev, FW_kNoDefaultPrinterMsg, message);
  369.         
  370.     FW_NoteAlert(partName, message);
  371. }
  372.  
  373. //----------------------------------------------------------------------------------------
  374. //    FW_CPrintHandler::NotifyPrintingCanceled
  375. //----------------------------------------------------------------------------------------
  376.  
  377. void FW_CPrintHandler::NotifyPrintingCanceled(Environment* ev)
  378. {
  379.     FW_CString partName;
  380.     fPart->GetPartName(ev, partName);
  381.     
  382.     FW_CString message;
  383.     ::FW_PrivLoadODFString(ev, FW_kPrintingCanceledMsg, message);
  384.  
  385.     FW_NoteAlert(partName, message);
  386. }
  387.  
  388. //----------------------------------------------------------------------------------------
  389. //    FW_CPrintHandler::PreModalDialog
  390. //----------------------------------------------------------------------------------------
  391.  
  392. FW_Boolean FW_CPrintHandler::PreModalDialog(Environment* ev)
  393. {
  394.     ODFrame* odFrame = fFrame->GetODFrame(ev);
  395.     ODSession* session = fPart->GetSession(ev);
  396.     ODToken modalFocus = session->Tokenize(ev, kODModalFocus);
  397.  
  398.     return session->GetArbitrator(ev)->RequestFocus(ev, modalFocus, odFrame);
  399. }
  400.  
  401. //----------------------------------------------------------------------------------------
  402. //    FW_CPrintHandler::PostModalDialog
  403. //----------------------------------------------------------------------------------------
  404.  
  405. void FW_CPrintHandler::PostModalDialog(Environment* ev)
  406. {
  407.     ODFrame* odFrame = fFrame->GetODFrame(ev);
  408.     ODSession* session = fPart->GetSession(ev);
  409.     ODToken modalFocus = session->Tokenize(ev, kODModalFocus);
  410.  
  411.     session->GetArbitrator(ev)->RelinquishFocus(ev, modalFocus, odFrame);
  412. }
  413.  
  414. //----------------------------------------------------------------------------------------
  415. // FW_CPrintHandler::GetPrintInfo
  416. //----------------------------------------------------------------------------------------
  417.  
  418. FW_CPrintInfo* FW_CPrintHandler::GetPrintInfo() const
  419. {
  420.     FW_CPrintInfo* printInfo = fPart->GetPrintInfo();
  421.  
  422. #if 0
  423.     if (printInfo == NULL)
  424.     {
  425.         printInfo = fPart->NewPrintInfo();
  426.         fPart->SetPrintInfo(printInfo);
  427.     }
  428. #endif
  429.     
  430.     return printInfo;
  431. }
  432.  
  433. //----------------------------------------------------------------------------------------
  434. // FW_CPrintHandler::CreateProgressDialog
  435. //----------------------------------------------------------------------------------------
  436.  
  437. void FW_CPrintHandler::CreateProgressDialog(
  438.         Environment*        ev,
  439.         ODFrame*            frame,
  440.         long                numPages,
  441.         const FW_CString&    title)
  442. {
  443. FW_UNUSED(frame);
  444.     FW_CPrintInfo* printInfo = GetPrintInfo();
  445.  
  446. #ifdef FW_SUPPORT_GX
  447.     gxJob job = printInfo->GetGXJob();
  448.     if (job != NULL)
  449.         return;
  450. #endif
  451. #ifdef FW_BUILD_WIN
  452.     FW_ASSERT(gWinProgressDlg == NULL);
  453.  
  454.     HWND hWndParent = WinGetFrameWindow(ev, frame);
  455.     gWinProgressDlg = ::CreateDialog(FW_gInstance,
  456.         FW_kWinPrintDialog, hWndParent, WinPrintProgessDlgProc);
  457.     FW_ASSERT(gWinProgressDlg);
  458.  
  459.     ::EnableWindow(hWndParent, FALSE);
  460.  
  461.     // Initialize some fields
  462.     ::SetDlgItemText(gWinProgressDlg, FW_kFileNameID, title);
  463.     ::SetDlgItemInt(gWinProgressDlg, FW_kTotalPagesNumID, numPages, TRUE);
  464.     ::ShowWindow(gWinProgressDlg, SW_NORMAL);
  465.     ::UpdateWindow(gWinProgressDlg);
  466.  
  467.     // Set up the abort proc
  468.     gWinAbortPrinting = FALSE;
  469.     ::SetAbortProc(lppd->hDC, WinAbortProc);
  470. #endif
  471. #ifdef FW_BUILD_MAC
  472.     FW_ASSERT(gMacProgressDlg == NULL);
  473.     
  474.     gMacPrintAborted = FALSE;
  475.  
  476.     FW_CSharedLibraryResourceFile resourceFile(ev);
  477.     gMacProgressDlg = ::GetNewDialog(FW_kMacPrintDialog, NULL, (WindowPtr) -1L);
  478.     
  479.     // Return if no Progress dialog resource available
  480.     if (!gMacProgressDlg) return;
  481.     
  482.     THPrint thPrint = printInfo->MacGetTHPrint();
  483.     gMacIdleProcUPP = NewPrIdleProc(MacIdleProc);
  484.     (*thPrint)->prJob.pIdleProc = gMacIdleProcUPP;
  485.     
  486.     ::HideDialogItem(gMacProgressDlg, FW_kCurrentPageStrID);
  487.     ::HideDialogItem(gMacProgressDlg, FW_kCurrentPageNumID);
  488.  
  489.     ::HideDialogItem(gMacProgressDlg, FW_kCancelingJobID);
  490.  
  491.     short iType;
  492.     Handle iHndl;
  493.     Rect iRect;
  494.     Str255 pasString;
  495.     
  496.     ::GetDialogItem(gMacProgressDlg, FW_kFileNameID, &iType, &iHndl, &iRect);
  497.     title.ExportPascal(pasString);
  498.     ::SetDialogItemText(iHndl, pasString);
  499.     ::SetWTitle(gMacProgressDlg, pasString);
  500.  
  501.     ::GetDialogItem(gMacProgressDlg, FW_kTotalPagesNumID, &iType, &iHndl, &iRect);
  502.     ::NumToString(numPages, pasString);
  503.     ::SetDialogItemText(iHndl, pasString);
  504.  
  505.     ::ShowWindow(gMacProgressDlg);
  506.     ::SelectWindow(gMacProgressDlg);
  507.     ::UpdateControls(gMacProgressDlg, gMacProgressDlg->visRgn);
  508.     ::UpdateDialog(gMacProgressDlg, gMacProgressDlg->visRgn);
  509. #endif
  510. }
  511.  
  512. //----------------------------------------------------------------------------------------
  513. // FW_CPrintHandler::DestroyProgressDialog
  514. //----------------------------------------------------------------------------------------
  515.  
  516. void FW_CPrintHandler::DestroyProgressDialog(
  517.         Environment*        ev,
  518.         ODFrame*            frame)
  519. {
  520. FW_UNUSED(ev);
  521. FW_UNUSED(frame);
  522.     FW_CPrintInfo* printInfo = GetPrintInfo();
  523.  
  524. #ifdef FW_SUPPORT_GX
  525.     gxJob job = printInfo->GetGXJob();
  526.     if (job != NULL)
  527.         return;
  528. #endif
  529. #ifdef FW_BUILD_WIN
  530.     FW_ASSERT(gWinProgressDlg != NULL);
  531.  
  532.     ::SetAbortProc(lppd->hDC, NULL);
  533.  
  534.     HWND hWndParent = WinGetFrameWindow(ev, frame);
  535.     ::EnableWindow(hWndParent, TRUE);
  536.     ::EndDialog(gWinProgressDlg, 0);
  537.     gWinProgressDlg = NULL;
  538. #endif
  539. #ifdef FW_BUILD_MAC
  540.     // Return if Progress dialog was not created
  541.     if (gMacProgressDlg == NULL) return;
  542.     
  543.     ::DisposeDialog(gMacProgressDlg);
  544.     gMacProgressDlg = NULL;
  545.     
  546.     DisposeRoutineDescriptor(gMacIdleProcUPP);
  547.     gMacIdleProcUPP = NULL;
  548. #endif
  549. }
  550.  
  551. //----------------------------------------------------------------------------------------
  552. // FW_CPrintHandler::UpdateProgressDialog
  553. //----------------------------------------------------------------------------------------
  554.  
  555. void FW_CPrintHandler::UpdateProgressDialog(
  556.         long                pageNumber)
  557. {
  558.     FW_CPrintInfo* printInfo = GetPrintInfo();
  559.  
  560. #ifdef FW_SUPPORT_GX
  561.     gxJob job = printInfo->GetGXJob();
  562.     if (job != NULL)
  563.         return;
  564. #endif
  565. #ifdef FW_BUILD_WIN
  566.     ::WinShowControl(gWinProgressDlg, FW_kStartingJobID,    FALSE);
  567.     
  568.     ::WinShowControl(gWinProgressDlg, FW_kTotalPagesStrID,    TRUE);
  569.     ::WinShowControl(gWinProgressDlg, FW_kTotalPagesNumID,    TRUE);
  570.     
  571.     ::WinShowControl(gWinProgressDlg, FW_kCurrentPageStrID,    TRUE);
  572.     ::WinShowControl(gWinProgressDlg, FW_kCurrentPageNumID,    TRUE);
  573.     
  574.     ::SetDlgItemInt(gWinProgressDlg, FW_kCurrentPageNumID, pageNumber, FALSE);
  575.     
  576.     ::UpdateWindow(gWinProgressDlg);
  577. #endif
  578. #ifdef FW_BUILD_MAC
  579.     // Return if Progress dialog was not created
  580.     if (!gMacProgressDlg) return;
  581.     
  582.     ::HideDialogItem(gMacProgressDlg, FW_kStartingJobID);
  583.     
  584.     ::ShowDialogItem(gMacProgressDlg, FW_kTotalPagesStrID);
  585.     ::ShowDialogItem(gMacProgressDlg, FW_kTotalPagesNumID);
  586.     
  587.     ::ShowDialogItem(gMacProgressDlg, FW_kCurrentPageStrID);
  588.     ::ShowDialogItem(gMacProgressDlg, FW_kCurrentPageNumID);
  589.  
  590.     short iType;
  591.     Handle iHndl;
  592.     Rect iRect;
  593.     Str255 pasString;
  594.     
  595.     ::GetDialogItem(gMacProgressDlg, FW_kCurrentPageNumID, &iType, &iHndl, &iRect);
  596.     ::NumToString(pageNumber, pasString);
  597.     ::SetDialogItemText(iHndl, pasString);
  598.  
  599.     ::UpdateControls(gMacProgressDlg, gMacProgressDlg->visRgn);
  600.     ::UpdateDialog(gMacProgressDlg, gMacProgressDlg->visRgn);
  601. #endif
  602. }
  603.  
  604. //----------------------------------------------------------------------------------------
  605. //    FW_CPrintHandler::HandlePageSetup
  606. //----------------------------------------------------------------------------------------
  607.  
  608. void FW_CPrintHandler::HandlePageSetup(Environment* ev)
  609. {
  610.     FW_TRY
  611.     {
  612.         FW_CPrintEnvironment printEnv;
  613.     
  614.         FW_CPrintInfo* printInfo = GetPrintInfo();
  615.         if (printInfo == NULL)
  616.         {
  617.             ODStorageUnit*    storageUnit = fPart->GetStorageUnit(ev);
  618.             printInfo = FW_NEW(FW_CPrintInfo, (ev, storageUnit, &printEnv));
  619.             fPart->SetPrintInfo(printInfo);
  620.         }
  621.     
  622.         if(DoPageSetupDialog(ev))
  623.         {
  624.             ODStorageUnit* storageUnit = fPart->GetStorageUnit(ev);
  625.             printInfo->SaveToStorageUnit(ev, storageUnit);
  626.         }
  627.     }
  628.     FW_CATCH_BEGIN
  629.     FW_CATCH_REFERENCE(FW_XException, pr)
  630.     {
  631.         if (pr.GetPlatformError() == FW_xNoDefaultPrinter)
  632.             NotifyNoDefaultPrinter(ev);
  633.         else
  634.             FW_THROW_SAME();
  635.     }
  636.     FW_CATCH_EVERYTHING()
  637.     {
  638.         FW_THROW_SAME();
  639.     }
  640.     FW_CATCH_END
  641. }
  642.  
  643. //----------------------------------------------------------------------------------------
  644. //    FW_CPrintHandler::HandlePrint
  645. //----------------------------------------------------------------------------------------
  646.  
  647. void FW_CPrintHandler::HandlePrint(Environment* ev)
  648. {
  649.     FW_TRY
  650.     {
  651.         FW_CPrintEnvironment printEnv;
  652.  
  653.         FW_CPrintInfo* printInfo = GetPrintInfo();
  654.         if (printInfo == NULL)
  655.         {
  656.             ODStorageUnit*    storageUnit = fPart->GetStorageUnit(ev);
  657.             printInfo = FW_NEW(FW_CPrintInfo, (ev, storageUnit, &printEnv));
  658.             fPart->SetPrintInfo(printInfo);
  659.         }
  660.  
  661.         if (DoJobDialog(ev))
  662.         {
  663.             fPrintView = CreatePrintView(ev);
  664.             PrintDocument(ev, &printEnv);
  665.         }
  666.     }
  667.     FW_CATCH_BEGIN
  668.     FW_CATCH_REFERENCE(FW_XException, pr)
  669.     {
  670.         switch(pr.GetPlatformError())
  671.         {
  672.             case FW_xNoDefaultPrinter:
  673.             {
  674.                 NotifyNoDefaultPrinter(ev);
  675.                 break;
  676.             }
  677.             case FW_xPrintingCanceled:
  678.             {
  679.                 NotifyPrintingCanceled(ev);
  680.                 break;
  681.             }
  682.             default:
  683.             {
  684.                 if (fPrintView != NULL)
  685.                 {
  686.                     ReleasePrintView(ev);
  687.                     fPrintView = NULL;
  688.                 }
  689.                 FW_THROW_SAME();
  690.             }
  691.         }
  692.     }
  693.     FW_CATCH_END
  694.     
  695.     if (fPrintView != NULL)
  696.     {
  697.         ReleasePrintView(ev);
  698.         fPrintView = NULL;
  699.     }
  700. }
  701.  
  702. //----------------------------------------------------------------------------------------
  703. //    FW_CPrintHandler::CreatePrintView
  704. //----------------------------------------------------------------------------------------
  705. //    return by default the content view
  706.  
  707. FW_CView* FW_CPrintHandler::CreatePrintView(Environment* ev)
  708. {
  709.     return fFrame->GetContentView(ev);
  710. }
  711.  
  712. //----------------------------------------------------------------------------------------
  713. //    FW_CPrintHandler::ReleasePrintView
  714. //----------------------------------------------------------------------------------------
  715.  
  716. void FW_CPrintHandler::ReleasePrintView(Environment* ev)
  717. {
  718. FW_UNUSED(ev);
  719.     // Does nothing
  720. }
  721.  
  722. //----------------------------------------------------------------------------------------
  723. //    FW_CPrintHandler::PrintDocument
  724. //----------------------------------------------------------------------------------------
  725.  
  726. void FW_CPrintHandler::PrintDocument(Environment* ev, FW_CPrintEnvironment* printEnv)
  727. {
  728.     long xPageCount, yPageCount;
  729.     CountPrintPages(ev, xPageCount, yPageCount);
  730.  
  731.     FW_ASSERT(xPageCount > 0);
  732.     FW_ASSERT(yPageCount > 0);
  733.  
  734.     FW_CPrintInfo* printInfo = GetPrintInfo();
  735.     FW_ASSERT(printInfo != NULL);
  736.  
  737.     short numCopies;
  738.     FW_Boolean collate;
  739.     printInfo->GetCopyCount(numCopies, collate);
  740.  
  741.     long firstPage, lastPage;
  742.     printInfo->GetPages(firstPage, lastPage);
  743.  
  744.     FW_ASSERT(firstPage > 0);
  745.     FW_ASSERT(lastPage > 0);
  746.  
  747.     long numPages = xPageCount * yPageCount;
  748.  
  749.     if (lastPage > numPages)
  750.     {
  751.         // prevent printing past last page
  752.         lastPage = numPages;
  753.     }
  754.     
  755.     // Get some OD objects that we will need
  756.     ODSession* session = fPart->GetSession(ev);
  757.     ODFrame* odFrame = fPrintView->GetFrame(ev)->GetODFrame(ev);
  758.  
  759.     FW_CPrintJob printJob(printInfo, this, printEnv);
  760.  
  761.     // Print the requested number of copies
  762.     short numCopiesPerPrintJob    = collate ? 1 : numCopies;
  763.     short numCopiesPerPage        = collate ? numCopies : 1;
  764.  
  765.     for (short copiesOfJob = 1; copiesOfJob <= numCopiesPerPrintJob; ++ copiesOfJob)
  766.     {
  767.         FW_CString jobTitle;
  768.         GetPrintJobTitle(ev, jobTitle);
  769.  
  770.         ODFacet* facet = printJob.OpenDocument(ev, odFrame, session, numPages, jobTitle);
  771.  
  772.         FW_TRY
  773.         {
  774.             for (long pageNumber = firstPage; pageNumber <= lastPage; ++ pageNumber)
  775.             {
  776.                 for (short copiesOfPage = 1; copiesOfPage <= numCopiesPerPage; ++ copiesOfPage)
  777.                 {
  778.                     printJob.OpenPage(pageNumber);
  779.  
  780.                     FW_TRY
  781.                     {
  782.                         long xPage = (pageNumber - 1) % xPageCount;
  783.                         long yPage = (pageNumber - 1) / xPageCount;
  784.  
  785.                         FW_CPrintInfo*    printInfo = GetPrintInfo();
  786.                     
  787.                         FW_CRect         pageBounds  = printInfo->GetPageBounds();
  788.                         FW_Fixed        pageWidth    = pageBounds.Width();
  789.                         FW_Fixed        pageHeight    = pageBounds.Height();
  790.                     
  791.                         pageBounds.Offset(FW_MultipliedByInt(pageWidth,  xPage), FW_MultipliedByInt(pageHeight, yPage));
  792.  
  793.                         PrintPage(ev, facet, pageBounds, xPage, yPage);
  794.                     }
  795.                     FW_CATCH_BEGIN
  796.                     FW_CATCH_EVERYTHING()
  797.                     {
  798.                         printJob.ClosePageOnError();
  799.                         FW_THROW_SAME();
  800.                     }
  801.                     FW_CATCH_END
  802.  
  803.                     printJob.ClosePage();
  804.                 }
  805.             }
  806.         }
  807.         FW_CATCH_BEGIN
  808.         FW_CATCH_EVERYTHING()
  809.         {
  810.             printJob.CloseDocumentOnError(ev, facet);
  811.             FW_THROW_SAME();
  812.         }
  813.         FW_CATCH_END
  814.  
  815.         printJob.CloseDocument(ev, facet);
  816.     }
  817.  
  818.     printJob.FinishPrinting();
  819. }
  820.  
  821. //----------------------------------------------------------------------------------------
  822. //    FW_CPrintHandler::CountPrintPages
  823. //----------------------------------------------------------------------------------------
  824.  
  825. void FW_CPrintHandler::CountPrintPages(Environment* ev, long& xPageCount, long& yPageCount)
  826. {
  827.     FW_CPrintInfo* printInfo = GetPrintInfo();
  828.     FW_ASSERT(printInfo != NULL);
  829.  
  830.     FW_CRect         pageBounds  = printInfo->GetPageBounds();
  831.     FW_Fixed        pageWidth    = pageBounds.Width();
  832.     FW_Fixed        pageHeight    = pageBounds.Height();
  833.  
  834.     FW_CPoint        content;
  835.     GetPrintContentExtent(ev, content);
  836.     
  837.     FW_ASSERT(content.x != FW_kFixed0);
  838.     FW_ASSERT(content.y != FW_kFixed0);
  839.  
  840.     xPageCount = FW_FixedToInt((content.x + FW_Half(pageWidth)  - FW_kFixedPos1) / pageWidth );
  841.     yPageCount = FW_FixedToInt((content.y + FW_Half(pageHeight) - FW_kFixedPos1) / pageHeight);
  842. }
  843.  
  844. //----------------------------------------------------------------------------------------
  845. //    FW_CPrintHandler::PrintPage
  846. //----------------------------------------------------------------------------------------
  847.  
  848. void FW_CPrintHandler::PrintPage(Environment* ev, ODFacet* facet, const FW_CRect& pageBounds, long xPage, long yPage)
  849. {
  850. FW_UNUSED(xPage);
  851. FW_UNUSED(yPage);
  852.     
  853.     // ----- Construct an appropriate transform and shape for this page
  854.     // Note: aqPageShape is in content coordinate 
  855.     FW_CAcquiredODTransform aqTransform = ::FW_NewODTransform(ev, pageBounds.TopLeft());
  856.  
  857.     FW_CAcquiredODShape    aqPageShape = ::FW_NewODShape(ev, pageBounds);
  858.  
  859.     // ----- Intersect the page shape with the print frame's extent
  860.     FW_CPoint content;
  861.     GetPrintContentExtent(ev, content);
  862.  
  863.     {
  864.         FW_CRect contentRect(FW_kFixed0, FW_kFixed0, content.x, content.y);
  865.         FW_CAcquiredODShape aqExtentShape = ::FW_NewODShape(ev, contentRect);
  866.         aqPageShape->Intersect(ev, aqExtentShape);
  867.     }
  868.     
  869.     // Transform from content to frame coordinate
  870.     fPrintView->ViewContentToFrame(ev, aqPageShape);
  871.  
  872.     // Take into account the position and scrolling of the print view
  873.     FW_CAcquiredODTransform aqContentToFrame = fPrintView->AcquireViewContentToFrameTransform(ev);
  874.     aqTransform->PostCompose(ev, aqContentToFrame);
  875.     
  876.     // Set the facet to use this transform and shape
  877.     aqTransform->Invert(ev);
  878.     facet->ChangeGeometry(ev, aqPageShape, aqTransform, NULL);
  879.     
  880.     // Print 
  881.     fPrintView->PrivSetTopPrintedView(ev, TRUE);
  882.     FW_TRY
  883.     {
  884.         // ----- First draw the printing view    
  885.         fPrintView->HandleDraw(ev, facet, aqPageShape);
  886.  
  887.         // ----- then draw all the embedded facets -----
  888.         FW_CFacetIterator ite(ev, facet);
  889.         for (ODFacet* embeddedFacet = ite.First(ev); ite.IsNotComplete(ev); embeddedFacet = ite.Next(ev))
  890.             embeddedFacet->Update(ev, NULL, NULL);
  891.     }
  892.     FW_CATCH_BEGIN
  893.     FW_CATCH_EVERYTHING()
  894.     {
  895.         fPrintView->PrivSetTopPrintedView(ev, FALSE);
  896.         FW_THROW_SAME();
  897.     }
  898.     FW_CATCH_END
  899.     
  900.     fPrintView->PrivSetTopPrintedView(ev, FALSE);
  901. }
  902.  
  903. #ifdef FW_BUILD_WIN
  904. //----------------------------------------------------------------------------------------
  905. // WinGetFrameWindow
  906. //----------------------------------------------------------------------------------------
  907.  
  908. HWND WinGetFrameWindow(
  909.         Environment*    ev,
  910.         ODFrame*        frame)
  911. {
  912.     ODFrameFacetIterator* ite = frame->->CreateFacetIterator(ev);
  913.     ODFacet* facet = ite->First(ev);
  914.     delete ite;
  915.     
  916.     return facet->GetWindow(ev)->GetPlatformWindow(ev);
  917. }
  918.  
  919. //----------------------------------------------------------------------------------------
  920. // WinShowControl
  921. //----------------------------------------------------------------------------------------
  922.  
  923. void WinShowControl(
  924.         HWND        hWnd,
  925.         int            nID,
  926.         BOOL        bShow)
  927. {
  928.     HWND hWndControl = ::GetDlgItem(hWnd, nID);
  929.     if (hWndControl != NULL)
  930.         ::ShowWindow(hWndControl, bShow ? SW_SHOW : SW_HIDE);
  931. }
  932.  
  933. //----------------------------------------------------------------------------------------
  934. // WinPrintProgessDlgProc
  935. //----------------------------------------------------------------------------------------
  936.  
  937. int CALLBACK WinPrintProgessDlgProc(
  938.         HWND        hWnd,
  939.         UINT        message,
  940.         WPARAM        wParam,
  941.         LPARAM        lParam)
  942. {
  943.     switch (message)
  944.     {
  945.     case WM_INITDIALOG:
  946.         return TRUE;
  947.  
  948.     case WM_COMMAND:
  949.         gWinAbortPrinting = TRUE;
  950.         
  951.         ::WinShowControl(gWinProgressDlg, FW_kStartingJobID,        FALSE);
  952.  
  953.         ::WinShowControl(gWinProgressDlg, FW_kTotalPagesStrID,    FALSE);
  954.         ::WinShowControl(gWinProgressDlg, FW_kTotalPagesNumID,    FALSE);
  955.  
  956.         ::WinShowControl(gWinProgressDlg, FW_kCurrentPageStrID,    FALSE);
  957.         ::WinShowControl(gWinProgressDlg, FW_kCurrentPageNumID,    FALSE);
  958.         
  959.         ::WinShowControl(gWinProgressDlg, FW_kCancelingJobID,    TRUE);
  960.         
  961.         ::UpdateWindow(gWinProgressDlg);
  962.         
  963.         return TRUE;
  964.     
  965.     default:
  966.         return FALSE;
  967.     }
  968. }
  969.  
  970. //----------------------------------------------------------------------------------------
  971. // WinAbortProc
  972. //----------------------------------------------------------------------------------------
  973.  
  974. BOOL CALLBACK WinAbortProc(
  975.             HDC        hDC,
  976.             int        nCode)
  977. {
  978.     MSG msg;
  979.     
  980.     while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  981.     {
  982.         if (gWinProgressDlg == NULL || !::IsDialogMessage(gWinProgressDlg, &msg))
  983.         {
  984.             ::TranslateMessage(&msg);
  985.             ::DispatchMessage(&msg);
  986.         }
  987.     }
  988.     
  989.     return !gWinAbortPrinting;
  990. }
  991. #endif
  992.  
  993. #ifdef FW_BUILD_MAC
  994.  
  995. //----------------------------------------------------------------------------------------
  996. // MacIdleProc
  997. //----------------------------------------------------------------------------------------
  998.  
  999. pascal void MacIdleProc()
  1000. {
  1001. #if 0
  1002.     short resFile = ::CurResFile();
  1003.  
  1004.     if (!gMacPrintAborted)
  1005.     {
  1006.         FW_Boolean bCancel = FALSE;
  1007.  
  1008.         FW_CMacTempPort tempPort = gMacProgressDlg;
  1009.  
  1010.         EventRecord event;
  1011.         RgnHandle rgnCursor = NULL;
  1012.         if (::WaitNextEvent(everyEvent, &event, 0, rgnCursor))
  1013.         {
  1014.             if (event.what == keyDown || event.what == autoKey)
  1015.             {
  1016.                 if ((event.message & charCodeMask) == '.')
  1017.                 {
  1018.                     if ((event.modifiers & cmdKey) != 0)
  1019.                     {
  1020.                         short             itemType;
  1021.                         ControlHandle     hItem = NULL;
  1022.                         Rect            box;
  1023.  
  1024.                         ::GetDialogItem(gMacProgressDlg, FW_kCancelButtonID, &itemType, (Handle*) &hItem, &box);
  1025.                         if (hItem != NULL)
  1026.                         {
  1027.                             long theTick;
  1028.                             ::HiliteControl(hItem, 1);
  1029.                             ::Delay(6,&theTick);
  1030.                             ::HiliteControl(hItem, 0);
  1031.                         }
  1032.  
  1033.                         bCancel = TRUE;
  1034.                     }
  1035.                 }
  1036.             }
  1037.             else if (::IsDialogEvent(&event))
  1038.             {
  1039.                 short nItemHit = 0;
  1040.                 DialogPtr dialog = NULL;
  1041.                 if (::DialogSelect(&event, &dialog, &nItemHit) &&
  1042.                     dialog == gMacProgressDlg &&
  1043.                     nItemHit == FW_kCancelButtonID)
  1044.                 {
  1045.                     bCancel = TRUE;
  1046.                 }
  1047.             }
  1048.         }
  1049.  
  1050.         if (bCancel)
  1051.         {
  1052.             // Update the dialog
  1053.             ::HideDialogItem(gMacProgressDlg, FW_kStartingJobID);
  1054.     
  1055.             ::HideDialogItem(gMacProgressDlg, FW_kTotalPagesStrID);
  1056.             ::HideDialogItem(gMacProgressDlg, FW_kTotalPagesNumID);
  1057.     
  1058.             ::HideDialogItem(gMacProgressDlg, FW_kCurrentPageStrID);
  1059.             ::HideDialogItem(gMacProgressDlg, FW_kCurrentPageNumID);
  1060.     
  1061.             ::ShowDialogItem(gMacProgressDlg, FW_kCancelingJobID);
  1062.     
  1063.             ::UpdateControls(gMacProgressDlg, gMacProgressDlg->visRgn);
  1064.             ::UpdateDialog(gMacProgressDlg, gMacProgressDlg->visRgn);
  1065.             
  1066.             // Tell the print manager that the job should be aborted
  1067.             ::PrSetError(iPrAbort);
  1068.             
  1069.             gMacPrintAborted = TRUE;
  1070.         }
  1071.     }
  1072.  
  1073.     ::UseResFile(resFile);
  1074. #endif
  1075. }
  1076.  
  1077. #endif
  1078.  
  1079.